package com.xukang.batch.service;

import org.activiti.engine.RuntimeService;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * 流程实例操作
 */
@Service
public class ProcessRuntimeService {

    @Autowired
    private RuntimeService runtimeService;

    /**
     * 根据流程定义 ID 启动流程实例
     */
    public ProcessInstance startProcessById(String processDefinitionId) {
        ProcessInstance instance = runtimeService.startProcessInstanceById(processDefinitionId);
        //act_run_task 表中就会有一条待办
        System.out.println("流程实例启动成功: ID=" + instance.getId()
                + ", 流程定义ID=" + instance.getProcessDefinitionId());
        return instance;
    }

    /**
     * 带参数启动流程
     */
    public ProcessInstance startProcessWithVariables(String processDefinitionKey, Map<String, Object> variables) {
        // 示例变量：申请人、申请金额等
        variables.put("applicant", "张三");
        variables.put("amount", 10000.00);
        ProcessInstance instance = runtimeService.startProcessInstanceByKey(processDefinitionKey, variables);
        //act_run_task 表中就会有一条待办
        return instance;
    }
}
